<template>
    <div>
        <headNav title="预览"  ></headNav>
        <img  class="face-img" :src="imgList[0]" alt="">
        <!-- {{imgList[0]}}
        <div>{{imgList}}</div> -->
        <div class="bottom-p">
            <div @click="confirm" class="comfirm-btn">确定提交</div>
            <div @click="select" class="select-btn">重新选择</div>
        </div>

       
        <!-- {{person}} -->
    </div>
</template>
<script>
import headNav from '@/components/headNav/headNav'
import {addFaceResource, updateStudent, updateTeacher} from '@/api/faceApi'
import imgUtil from '@/util/imgUtil'
export default {
    components: {
        headNav
    },
    computed: {
        imgList(){
            return this.$store.state.media.imgDataSource
        },
        faceTeacher () {
            return this.$store.state.myInfo.faceTeacher
        }
    },
    data(){
        return {
            person: '',
          
        }
    },
    methods: {
        preview(){
            wx.previewImage({
                current: this.imgList[0],
                urls: this.imgList,
                fail: (err) => {
                    alert(err)
                }   
            });
        },
        async addFaceResource () {
           

            var person = {
                externalFields: {
                  externalFields: {
                          schoolId: this.faceTeacher.schoolId,
                          className: '',
                          personId: this.faceTeacher.id,
                        
                          name: '',
                          faceImgUrl: '',
                          gender: this.faceTeacher.gender ? 1 : 0,
                          personType: 0   // 教师：0，学生：1
                  }
                },
                faceSetName: this.faceTeacher.schoolId,
                personName: this.faceTeacher.name,
                imageUrl: this.$store.state.media.imgData[0],
                living: true
              }
              this.person = person
              let res = await addFaceResource(person)

              if (res.status != 200) {
                throw new Error(res.msg);
              } 

             
        },
       
        async updateTeacher () {
            await updateTeacher({
                id: this.faceTeacher.id,
                faceImgUrl: this.$store.state.media.imgData[0],
                checkUpdateTime: true
            })
        },
        async confirm () {
            try {
                this.$vux.loading.show({
                    text: '加载中'
                })

                await this.$store.dispatch('media/startUploadImage')
            
                await this.$store.dispatch('media/uploadServeId')

                let res = await imgUtil.checkImgForHuaWei(this.$store.state.media.imgData[0])
                if(!res.flag){
                    this.$vux.toast.text(res.msg, 'middle')
                    return
                }
                
                await this.addFaceResource()
                // await this.updateTeacher()

                this.$router.go(-1)

                this.$vux.alert.show({
                    title: '提示',
                    content: '面部识别照片上传成功',
                })
            } catch (err) {
                console.log(JSON.stringify(err))
                this.$vux.alert.show({
                    title: '错误',
                    content: err.message,
                })
            }
            

        },
        select () {
            this.$store.dispatch('media/getFaceImg')
        }
    }
}
</script>
<style scoped>
.face-img{
    width: 6rem; height: 8rem;
    margin: 0.2rem auto;
    display: block;
    object-fit: contain;
}
.bottom-p{
    padding: 0.2rem 0 0 0;
    text-align: center;
}
.comfirm-btn{
    background: #128FEF; color: #FFF;
    text-align: center; padding: 0.2rem 0;
    width: 3rem; margin: 0 0.2rem; display: inline-block;
    font-size: 0.32rem; 
}
.select-btn{
    border:1px solid #128FEF; color: #128FEF;
    text-align: center; padding: 0.2rem 0;
    width: 3rem; margin: 0 0.2rem; display: inline-block;
    font-size: 0.32rem; 
}

</style>
